﻿//-----------------------------------------------------------------------
// <copyright file="D3dImageSlimDX.cs" company="Flaxen Geo">
//    Copyright (c) 2011, Michael P. Gerlek.  All rights reserved.
// </copyright>
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, 
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// * Neither the name of Flaxen Geo nor the names of its contributors may be 
//   used to endorse or promote products derived from this software without 
//   specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------------

namespace Flaxen.SlimDXControlLib
{
    using System;
    using System.Windows;
    using System.Windows.Interop;
    using SlimDX.Direct3D9;

    /// <summary>
    /// This class extends D3DImage to support D3D/DirectX10-based rendering using SlimDX,
    /// so that it is easier to implement the 3D content provider code.
    /// </summary>
    internal class D3DImageSlimDX : D3DImage, IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the D3DImageSlimDX class.
        /// </summary>
        public D3DImageSlimDX()
        {
            D3DDeviceManager.Create();
        }

        /// <summary>
        /// Called by the SlimDXControl object to invalidate the buffer.
        /// </summary>
        public void InvalidateD3DImage()
        {
            if (D3DDeviceManager.SharedTexture != null)
            {
                Lock();
                AddDirtyRect(new Int32Rect(0, 0, PixelWidth, PixelHeight));
                Unlock();
            }
        }
       
        /// <summary>
        /// Called by the SlimDXControl object to invalidate the buffer.
        /// </summary>
        /// <param name="texture">the texture to be put into the buffer</param>
        public void SetBackBufferSlimDX(SlimDX.Direct3D10.Texture2D texture)
        {
            D3DDeviceManager.ClearSharedTexture();

            if (texture == null)
            {
                Lock();
                SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero);
                Unlock();
                return;
            }

            if (!IsShareable(texture))
            {
                throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared");
            }

            D3DDeviceManager.CreateSharedTexture(texture);
            using (Surface surface = D3DDeviceManager.SharedTexture.GetSurfaceLevel(0))
            {
                Lock();
                SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.ComPointer);
                Unlock();
            }

            return;
        }

        /// <summary>
        /// Disposes of the D3D resources.
        /// </summary>
        public void Dispose()
        {
            SetBackBufferSlimDX(null);
            D3DDeviceManager.Destroy();
        }

        private static bool IsShareable(SlimDX.Direct3D10.Texture2D texture)
        {
            return (texture.Description.OptionFlags & SlimDX.Direct3D10.ResourceOptionFlags.Shared) != 0;
        }
    }
}
